﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Joyeon.Utils
{
    /// <summary>
    /// 讲一个对象序列化为一个Json字符串，或者将一个Json字符串序列化为一个对象
    /// </summary>
    public static class JsonUtil
    {
        private static System.Web.Script.Serialization.JavaScriptSerializer jss =
         new System.Web.Script.Serialization.JavaScriptSerializer();

        /// <summary>
        /// 将一个对象序列成Json字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String Serialize(Object obj)
        {
            return jss.Serialize(obj);
        }
        /// <summary>
        /// 将一个字符串反序列化为泛型类型指定的类的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="s"></param>
        /// <returns></returns>
        public static T Deserialize<T>(String s) where T : class
        {
            return jss.Deserialize<T>(s);
        }

        /// <summary>
        /// 将一个字符串反序列化为指定类型的类的对象
        /// </summary>
        /// <param name="s"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        public static Object Deserialize(String s, Type targetType)
        {
            return jss.Deserialize(s, targetType);
        }
    }
}
